﻿$(function () {
    $("#submit").click(function () {
        Submit();
    });

    $("#body").keypress(function (event) {
        if (event.which == '13') {
            Submit();
        }
    });
});

function Submit() {

    $("#submit").hide();

    var input = {
        ID: $("#id").val(),
        Name: $("#Name").val(),
        Time: $("#Time").val(),
        Location: $("#Location").val(),
        DisplayOrder: $("#DisplayOrder").val()
    };

    Ajax({
        url: "/EidPrayers/SavePrayer",
        data: JSON.stringify(input),
        success: function (data) {
            if (data.IsValid == false) {
                ShowValidationErrors(data);
            } else if (data.IsSaved == true) {
                location.href = "/EidPrayers?msg=3";
            }
        },
        complete: function () {
            $("#submit").show();
        }
    });
}

function ValidationErrors(data) {

    var msg = "";
    $("#Nameval").remove();
    $("#Timeval").remove();
    $("#Locationval").remove();
    $("#DisplayOrderval").remove();

    if (data.NameValidationCode > 0) {
        if (data.NameValidationCode == 1) {
            msg = "Please enter";
        } else if (data.NameValidationCode == 2) {
            msg = "cannot be more than 50 characters";
        }
        $("#Name").focus();
        $("#Name").after('<span id="Nameval" class="input-notification error png_bg">' + msg + '</span>');
    }

    if (data.TimeValidationCode > 0) {
        if (data.TimeValidationCode == 1) {
            msg = "Please enter";
        } else if (data.TimeValidationCode == 2) {
            msg = "cannot be more than 50 characters";
        }
        $("#Time").focus();
        $("#Time").after('<span id="Timeval" class="input-notification error png_bg">' + msg + '</span>');
    }

    if (data.LocationValidationCode > 0) {
        if (data.LocationValidationCode == 1) {
            msg = "Please choose";
        }
        $("#Location").focus();
        $("#Location").after('<span id="Locationval" class="input-notification error png_bg">' + msg + '</span>');
    }

    if (data.DisplayOrderValidationCode > 0) {
        if (data.DisplayOrderValidationCode == 1) {
            msg = "Please enter";
        } else if (data.DisplayOrderValidationCode == 2) {
            msg = "must be a number";
        }
        $("#DisplayOrder").focus();
        $("#DisplayOrder").after('<span id="DisplayOrderval" class="input-notification error png_bg">' + msg + '</span>');
    }
}